package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        //由于页面中没有给创建时间和状态添加文本框，因此需要手动设置或给默认值
        statement.setCreateTime(new Date());
        //默认状态为消费中
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        //只要是消费中的状态才可以进行编辑
        Statement oldObj = this.get(statement.getId());
        if(!Statement.STATUS_CONSUME.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于消费中的状态才可以进行编辑");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String statementIds) {
        Long[] dictIds = Convert.toLongArray(statementIds);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
            //在删除结算单数据时也需要把关联的结算单明细一并删除
            statementItemMapper.deleteItemByStatmentId(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void payStatement(Long statementId) {
        //如果该状态为消费中，才可点击确认支付
        Statement statement = this.get(statementId);
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("已支付的结算单不能重复支付");
        }
        //成功，将状态修改为已支付
        //支付时间在sql语句中进行通过now()函数进行更新
        statementMapper.updatePayStatement(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        //如果结算单是预约单生成的,需要根据预约单ID更新预约单的状态为已结算
        Long appointmentId = statement.getAppointmentId();
        //如果预约单id不为空，需要根据预约单ID更新预约单的状态为已结算
        if(appointmentId != null){
            //说明是预约单生成的
            appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        //通过id查询预约对象，判断其状态
        Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
        Integer status = appointment.getStatus();
        if(!(Appointment.STATUS_ARRIVAL.equals(status) || Appointment.STATUS_SETTLE.equals(status))){
            throw new BusinessException("只有结算单和到店状态才可点击结算单按钮");
        }
        //根据预约单ID去结算单表中查询是否已经有记录
        Statement statement = statementMapper.queryAppointmentId(appointmentId);
        //如果不能找到,说明之前没有生成过结算单,把预约单信息封装成结算单对象,并插入到结算单中
        if(statement == null){
            statement = new Statement();
            statement.setCreateTime(new Date());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
            statement.setCarSeries(appointment.getCarSeries());//汽车类型
            statement.setCustomerName(appointment.getCustomerName());//客户名称
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setAppointmentId(appointment.getId());//预约单ID
            statement.setServiceType(appointment.getServiceType().longValue());//服务类型
            statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
            statementMapper.insert(statement);
            //保存成功后把预约单状态修改成结算单生成
            appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        }
        return statement;
    }


}
